/*
	This file is part of CLISRServer.
	Foobar is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	CLISRServer is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with CLISRServer.  If not, see <http://www.gnu.org/licenses/>.
*/


//!TestClass for the class SRPluginLoader
/*!Contains a test harness for the SRPluginLoader class

*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>
#include "SRPluginLoader.h"
#include <vector> 
//#include "SRPluginLoaderTest.h"
//#include "SRServer.h"
using namespace std;
SRPluginLoader *srp;

int init_suite1(void)
{
//SRPluginLoader *srp = new SRPluginLoader("../clisrserver/plugins/vlc/plugin.so"); 
SRPluginLoader *srp = new SRPluginLoader("../clisrserver/plugins/example/source/plugin.so"); 		
   return 0;
}

/* The suite cleanup function.
 * Closes the temporary file used by the tests.
 * Returns zero on success, non-zero otherwise.
 */
int clean_suite1(void)
{
  return 0;
}

//! Test for the SRPluginLoader::loadPlugin() method
/*! Tests that the functionloads the plugins, by feeding the path and checking that the return is not NULL
*/

void testloadPlugin(void)
{
CU_ASSERT (srp->loadPlugin("../../../clisrserver/plugins/vlc/plugin.so") != NULL);
}


/*void testgetPlugins(void)
{
	SRPluginLoader *srp = new SRPluginLoader("../../clisrserver/plugins/example/source/libmean.so"); 
	srp->loadPlugin("../../clisrserver/plugins/example/source/libmean.so");
	CU_ASSERT(srp->getPlugins() != NULL);
}	
*/
//! Test for the SRPluginLoader::getLastPlugin() method
/*! Tests that the function returns the latest loaded plugin; Right now not really implemented correctly 
*/

void testgetLastPlugin(void)
{
	SRPluginLoader *srp = new SRPluginLoader("../../../clisrserver/plugins/example/source/plugin.so"); 
	SRPlugin* plug1 = srp-> loadPlugin("../../../clisrserver/plugins/vlc/plugin.so");
	SRPlugin* plug2 = srp-> loadPlugin("../../../clisrserver/plugins/mediabrowser/source/MBBrowser.so");
	SRPlugin* plugin = srp -> getLastPlugin();
	CU_ASSERT(plugin != plug1);
	CU_ASSERT(srp->getLastPlugin() != NULL);
}	

//! Test for the SRPluginLoader::exec(char * command, SRPLugin* plug) method
/*! Tests that it is possible to call the doCommand for a loaded plugin through the SEPLuginLoader::exec(char * command, SRPLugin* plug) method;
*/
void testexec(void)
 {
	SRPluginLoader *loader = new SRPluginLoader("../../../clisrserver/plugins/vlc/plugin.so");
	SRPlugin* plug = loader -> getLastPlugin();
	char * command = "play-/home/paladi/sample_mpeg4.mp4";
	CU_ASSERT(loader -> exec(command, plug) == "done play");
 } 



//! Test for the SRPluginLoader::getPlugins() method
/*! Test that the SRPLuginLoader::getPlugins() method returns exactly the number of loaded plugins; assumes that there are four plugins available to be loaded;
*/

void testgetPlugins(void)
{
	SRSettings *settings = new SRSettings();
	settings->setPort(2345);
	settings->setPluginsPath("../../../clisrserver/plugins");
	settings->setRemoteLayoutsPath("");
	SRPluginLoader *loader = new SRPluginLoader(settings);
	std::vector <SRPlugin*>* loadedPlugins = loader -> getPlugins();
	CU_ASSERT(loadedPlugins->size() == 4);
}


//! Test for the SRPluginLoader::loadPlugins() method
/*! Test that the SRPLuginLoader::looadPlugins() loads the .so plugins that are available in the subdirectories of the path directory given to it; 
*/


void testloadPlugins()
{
	SRSettings *settings = new SRSettings();
	settings->setPort(2345);
	settings->setPluginsPath("../../../clisrserver/plugins");
	settings->setRemoteLayoutsPath("");
	SRPluginLoader *loader = new SRPluginLoader(settings);
	std::vector <SRPlugin*>* loadedPlugins = loader -> getPlugins();
	CU_ASSERT(loader -> getLastPlugin() != NULL);
	CU_ASSERT(loadedPlugins->size() != 0);
}

/* The main() function for setting up and running the tests.
 * Returns a CUE_SUCCESS on successful running, another
 * CUnit error code on failure.
 */
int main()
{
   CU_pSuite pSuite = NULL;

   /* initialize the CUnit test registry */
   if (CUE_SUCCESS != CU_initialize_registry())
      return CU_get_error();

   /* add a suite to the registry */
   pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
   if (NULL == pSuite) {
      CU_cleanup_registry();
      return CU_get_error();
   }

   /* add the tests to the suite */
   /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
   if ((NULL == CU_add_test(pSuite, "test of loadPlugin()", testloadPlugin)) ||
	(NULL == CU_add_test(pSuite, "test of loadPlugins()", testloadPlugins)) ||
       (NULL == CU_add_test(pSuite, "test of getLastPlugin()", testgetLastPlugin)) ||
	(NULL == CU_add_test(pSuite, "test of getPlugins()", testgetPlugins)) ||
 	(NULL == CU_add_test(pSuite, "test of exec()", testexec)))
   {
      CU_cleanup_registry();
      return CU_get_error();
   }

   /* Run all tests using the CUnit Basic interface */
   CU_basic_set_mode(CU_BRM_VERBOSE);
   CU_basic_run_tests();
   CU_cleanup_registry();
   return CU_get_error();
}
